<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="user" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          :default-expand-all="true"
          :highlight-current="isTree"
          expand-on-click-node
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="scope">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <svg-icon v-if="scope.data.children.length===0" icon-class="user" />
                <svg-icon v-else :icon-class="scope.node.expanded ? 'jian' : 'jia'" />
                <span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(scope.data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(scope.data.id)">删除部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(scope.data.id)">编辑部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- dialog弹窗 -->
    <el-dialog :visible.sync="shouDialog" :title="(isEdit?'编辑':'新增')+'部门'">
      <!-- 弹窗的表单 -->
      <deptDialog
        v-if="shouDialog"
        :is-edit="isEdit"
        :pid="pid"
        :origin-list="originList"
        @reset="reset"
        @editDone="editDone"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getDeptList, delDept } from '@/api/department'
import { array2Tree } from '@/utils/index'
import deptDialog from './deptDialog'
export default {
  components: { deptDialog },
  // 1.数据
  data() {
    return {
      // 1.1部门列表
      list: [],
      // 1.2dialog弹窗的布尔值
      shouDialog: false,
      // 1.3 点击操作的pid
      pid: '',
      // 1.4新增还是编辑布尔值
      isEdit: false,
      // 1.5传递给子组件校验的数据
      originList: [],
      // 1.6树展开状态的布尔
      isTree: true
    }
  },
  // 2.created
  created() {
    this.loadDept()
  },
  // 3.方法
  methods: {
    // 3.1获取组织部门列表
    async loadDept() {
      const res = await getDeptList()
      // shift去掉数组的第一项,会改变原数组
      res.data.depts.shift()
      this.list = array2Tree(res.data.depts, '')
      this.originList = res.data.depts.map(item => {
        return {
          pid: item.pid,
          id: item.id,
          code: item.code,
          name: item.name
        }
      })
    },
    // 3.2点击添加子部门
    hAdd(id) {
      // (1)显示弹框
      this.shouDialog = true
      // (2)清空表单的数据
      this.pid = id
      this.isEdit = false
    },
    // 3.3子组件通知的事件
    async reset() {
      this.shouDialog = false
      await this.loadDept()
    },
    // 3.4 删除部门
    hDel(id) {
      this.$confirm('请问您要删除吗?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDept(id)
        // 提示+刷新
        this.$message.success('删除成功')
        this.loadDept()
      }).catch(() => {
      })
    },
    // 3.5编辑部门
    hEdit(id) {
      // (1)显示弹窗 +传递id
      this.shouDialog = true
      this.pid = id
      this.isEdit = true
    },
    // 3.6子组件通知的事件
    editDone() {
      // 关闭弹窗+刷新数据
      this.shouDialog = false
      this.loadDept()
    }
  }
}
</script>
